package com.zzedu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.cache.CacheNameContansts;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.spring.SpringUtils;
import com.zzedu.system.domain.SysRegion;
import com.zzedu.system.mapper.SysRegionMapper;
import com.zzedu.system.service.ISysRegionService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 省市区信息Service业务层处理
 *
 * @author Mr.Wang
 * @date 2023-03-08
 */
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements ISysRegionService {
    public final static String cacheName = CacheNameContansts.SYS_REGION;

    @Override
    @Cacheable(value = cacheName, key = "'region:'+#p0")
    public SysRegion getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @Cacheable(value = cacheName, key = "#p0+':'+#p1+':'+#p2")
    public List<SysRegion> query(Long regionLevel, String parentId, String regionId) {
        return this.list(Wrappers.lambdaQuery(SysRegion.class)
                .eq(null != regionLevel, SysRegion::getRegionLevel, regionLevel)
                .eq(StringUtils.isNotBlank(parentId), SysRegion::getParentId, parentId).
                eq(StringUtils.isNotBlank(regionId), SysRegion::getRegionId, regionId));
    }

    public List<String> provinceAndCityAndDistrict(List<String> regIds) {
        List<String> provinceAndCityAndDistrict = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(regIds)) {
            for (String id : regIds) {
                if (!StringUtils.isNotBlank(id)) {
                    continue;
                }
                SysRegion sysRegion = SpringUtils.getAopProxy(this).getById(id);
                if (null != sysRegion) {
                    provinceAndCityAndDistrict.add(sysRegion.getRegionName());
                }
            }
        }
        return provinceAndCityAndDistrict;
    }

    @Override
    @Cacheable(value = cacheName, key = "#region.regionLevel+':'+#region.parentId")
    public List<SysRegion> selectRegion(SysRegion region) {
        List<SysRegion> sysRegionList = Lists.newArrayList();
        ISysRegionService sysRegionService = SpringUtils.getAopProxy(this);
        this.baseMapper.selectList(Wrappers.lambdaQuery(SysRegion.class)
                        .in(null != region.getRegionLevel(), SysRegion::getRegionLevel, region.getRegionLevel())
                        .in(StringUtils.isNotBlank(region.getParentId()), SysRegion::getParentId, region.getParentId())
                , new ResultHandler<SysRegion>() {
                    @Override
                    public void handleResult(ResultContext<? extends SysRegion> resultContext) {
                        SysRegion region = resultContext.getResultObject();
                        region.setLeaf(!sysRegionService.exists(Wrappers.lambdaQuery(SysRegion.class).eq(SysRegion::getParentId, region.getRegionId())));
                        sysRegionList.add(region);
                    }
                }
        );
        return sysRegionList;
    }
}
